package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.AlarmConfigBean;
import com.emip.dao.AlarmConfigDAO;
import com.emip.service.AlarmConfigService;

@Service("alarmConfigService")
public class AlarmConfigServiceImpl implements AlarmConfigService {

	@Autowired
	AlarmConfigDAO alarmConfigDAO;
	
	public AlarmConfigBean addAlarmConfig(String alarmID, String alarmName,
			int sendTimes) {
		AlarmConfigBean newBean = new AlarmConfigBean();
		newBean.setAlarmID(alarmID);
		newBean.setAlarmName(alarmName);
		newBean.setSendTimes(sendTimes);
		//查重，是否已经有相同的alarmID, alarmName的记录存在
		//若有重复记录，则返回null
		if (alarmConfigDAO.findExist(newBean)) {
			return null;
		}
		int newID = alarmConfigDAO.add(newBean);
		newBean.setId(newID);
		return newBean;
	}

	public Boolean deleteAlarmConfig(int id) {
		AlarmConfigBean delBean = alarmConfigDAO.findByID(id);
		alarmConfigDAO.remove(delBean);
		return true;
	}

	public AlarmConfigBean getAlarmConfigByID(int id) {
		return alarmConfigDAO.findByID(id);
	}

	public List<AlarmConfigBean> getAll() {
		return alarmConfigDAO.getAll();
	}

	public AlarmConfigBean updateAlarmConfig(int id, String alarmID,
			String alarmName, int sendTimes) {
		AlarmConfigBean newBean = alarmConfigDAO.findByID(id);
		newBean.setAlarmID(alarmID);
		newBean.setAlarmName(alarmName);
		newBean.setSendTimes(sendTimes);
		//查重，是否已经有相同的alarmID, alarmName的记录存在
		//若有重复记录，则返回null
		if (alarmConfigDAO.findExist(newBean)) {
			return null;
		}
		alarmConfigDAO.update(newBean);
		return newBean;
	}

}
